package com.android.launcher3.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.launcher3.AppInfo;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.manager.WidgetManager;

public class FirstScreenLayout2 extends FrameLayout {

    private final static double SCALE_LAND = 3.17;
    private final static double SCALE_PORTRAIT = 0.85;
    private final static double SCALE_SQUARE = 1.0;
    private RecommendWidget recommendWidget1;
    private RecommendWidget recommendWidget2;
    private RecommendWidget recommendWidget3;
    private View gap1;
    private View gap2;
    private View gap3;
    private View gap4;
    private RecommendWidget recommendWidget4;

    public FirstScreenLayout2(@NonNull Context context) {
        super(context);
        init();
    }

    public FirstScreenLayout2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FirstScreenLayout2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.first_screen_layout2, this, true);
        recommendWidget1 = findViewById(R.id.recommend1);
        recommendWidget2 = findViewById(R.id.recommend2);
        recommendWidget3 = findViewById(R.id.recommend3);
        gap1 = findViewById(R.id.gap1);
        gap2 = findViewById(R.id.gap2);
        gap3 = findViewById(R.id.gap3);
        gap4 = findViewById(R.id.gap4);
        recommendWidget4 = findViewById(R.id.recommend4);
        screenAdaption();
    }

    public void bindWidget() {
        bindWidgetInner(recommendWidget1);
        bindWidgetInner(recommendWidget2);
        bindWidgetInner(recommendWidget3);
        bindWidgetInner(recommendWidget4);

    }

    public void notifyMapWidgetChanged(String pkgName,String className) {

    }

    private void bindWidgetInner(@NonNull RecommendWidget recommendWidget) {
        recommendWidget.bindWidget();
    }

    private void screenAdaption() {

        if (Utilities.isLongStripScreen(getContext())) {
            gap4.setVisibility(VISIBLE);
            recommendWidget4.setVisibility(VISIBLE);
            return;
        }

        double designScale = Utilities.isSquareScreen(getContext()) ? SCALE_SQUARE :
                (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? SCALE_PORTRAIT : SCALE_LAND);

        post(() -> {
            int width = getWidth();
            int height = getHeight();

            double scale = (width * 1.0) / (height * 1.0);

            if (scale > designScale) {
                width = (int) (height * designScale);
            } else {
                height = (int) ((width * 1.0) / designScale);
            }

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
            layoutParams.width = width;
            layoutParams.height = height;
            setLayoutParams(layoutParams);

            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

            if (Utilities.isSquareScreen(getContext())) {

                LinearLayout.LayoutParams gap = new LinearLayout.LayoutParams(Utilities.pxFromDp(30, displayMetrics), Utilities.pxFromDp(30, displayMetrics));
                gap1.setLayoutParams(gap);
                gap3.setLayoutParams(gap);
            }

        });

    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            screenAdaption();
        }
    }
}

